package com.winchain.chainbill.basic.cus.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.winchain.chainbill.basic.cus.dao.CusRecDao;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.basic.cus.vo.BasCustomerVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("basCusService")
public class CusServiceImpl extends PersistentServiceImpl<CusRec> implements CusRecService {
	@Autowired
	private CusRecDao basCustomerDao;

	@Pagination
	public List<BasCustomerVo> findCustomersByPage(PageLimit limit, BasCustomerVo basCustomerVo) {
		if (StringUtil.notTrimEmpty(basCustomerVo.getSearchValue())) {
			basCustomerVo.setSearchValue(StringUtil.getParameterLike(basCustomerVo.getSearchValue()));
		}
		if (StringUtil.notTrimEmpty(basCustomerVo.getCusType())) {
			String[] cusTypes = basCustomerVo.getCusType().split(",");
			if (cusTypes.length > 0) {
				basCustomerVo.setCusTypes(cusTypes);
				basCustomerVo.setCusType("");
			}
		}
		basCustomerVo.setCusState("E");
		return this.basCustomerDao.findCustomers(basCustomerVo);
	}

	public CusRec getCustomer(CusRec basCustomer) {
		return (CusRec) this.basCustomerDao.selectOne(basCustomer);
	}

	public CusRec findCustomerByAcctNo(String acctNo) {
		return this.basCustomerDao.findCustomerByAcctNo(acctNo);
	}

	@Pagination
	public List<BasCustomerVo> findRelCussByPage(PageLimit limit, BasCustomerVo customer) {
		if (StringUtil.notTrimEmpty(customer.getSearchValue())) {
			customer.setSearchValue(StringUtil.getParameterLike(customer.getSearchValue()));
		}
		if (StringUtil.notTrimEmpty(customer.getCusType())) {
			String[] cusTypes = customer.getCusType().split(",");
			if (cusTypes.length > 0) {
				customer.setCusTypes(cusTypes);
				customer.setCusType("");
			}
		}
		customer.setCusState("E");
		customer.setRelType("1");
		return this.basCustomerDao.findRelCusList(customer);
	}

	public List<CusRec> selectXfeOrDctCusListByGrpIds(String... grpIds) {
		return this.basCustomerDao.selectXfeOrDctCusListByGrpIds(grpIds);
	}

	public CusRec getPlatform(String cusId) {
		List<CusRec> recs = this.basCustomerDao.getPlatform(cusId, "OPERATOR");
		if ((recs != null) && (recs.size() > 0)) {
			return (CusRec) recs.get(0);
		}
		return null;
	}

	public CusRec selectByName(String name) {
		CusRec cusRec = new CusRec();
		cusRec.setCusName(name);
		cusRec = (CusRec) selectOne(cusRec);
		return cusRec;
	}

	public List<CusRec> selectListByName(String name) {
		name = StringUtil.getParameterLike(name);
		return this.basCustomerDao.selectListByName(name);
	}

	public CusRec getArtisan(String cusId) {
		List<CusRec> recs = this.basCustomerDao.getPlatform(cusId, "ARTISAN");
		if ((recs != null) && (recs.size() > 0)) {
			return (CusRec) recs.get(0);
		}
		return null;
	}

	public List<CusRec> selectFisrSupplierByInviterId(String inviterId, String grpId) {
		return this.basCustomerDao.selectFisrSupplierByInviterId(inviterId, grpId);
	}

	public List<CusRec> selectCusByBizCode(String grpId, String tagCode, String bizCode) {
		return this.basCustomerDao.selectCusByBizCode(grpId, tagCode, bizCode);
	}
}
